package br.com.fiap.dao.impl;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.fiap.dao.LoginDAO;
import br.com.fiap.entity.Cliente;
import br.com.fiap.entity.Login;

public class LoginDAOImpl extends AbstractDAOImpl<Login> implements LoginDAO {

	private static final long serialVersionUID = -2639710791058940976L;

	@Override
	public Login findByName(String login) {

		StringBuilder queryStr = new StringBuilder();

		queryStr.append(" select c from " + Login.class.getCanonicalName() + " c ");

		queryStr.append(" where lower(login) = :login ");
	
		Session session = getHibernateSession();
		Query query = session.createQuery(queryStr.toString());

		query.setParameter("login", login);
		
		Login loginPersisted = (Login) query.uniqueResult();
		
		session.clear();
		session.close();

		return loginPersisted;
	}

	public Login findBySenha(String senha) {

		StringBuilder queryStr = new StringBuilder();

		queryStr.append(" select c from " + Login.class.getCanonicalName() + " c ");

		queryStr.append(" where lower(senha) = :senha ");
	
		Session session = getHibernateSession();
		Query query = session.createQuery(queryStr.toString());

		query.setParameter("senha", senha);
		
		Login loginPersisted = (Login) query.uniqueResult();
		
		session.clear();
		session.close();

		return loginPersisted;
	}
	
	@Override
	public Class<Login> getEntityClass() {
		return Login.class;
	}

}
